package com.stu.servlet;

import com.stu.dao.StudentDao;
import com.stu.dao.impl.ManageDaoImpl;
import com.stu.dao.impl.StudentDaoImpl;
import com.stu.model.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/MainPageServlet")
public class MainPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //获取用户的动作
        String action = req.getParameter("action");
        StudentDaoImpl studentDao = new StudentDaoImpl();
        ManageDaoImpl manageDao = new ManageDaoImpl();
        User user = (User) req.getSession().getAttribute("session_newUser");
        String userAccount = user.getUserAccount();
        //如果用户的动作为index,则跳转回index页面即首页
        if (action.equals("index")) {
            req.setAttribute("mainRight", "blank.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
            //查看个人的具体信息
        } else if (action.equals("goMyDetail")) {
            req.setAttribute("mainRight", "detail.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
              //修改自己的密码
        } else if (action.equals("upPassword")) {
            req.getRequestDispatcher("upPassword.jsp").forward(req, resp);
            //确认修改自己的密码
        } else if (action.equals("sureUpPassword")) {
            String userPassword = req.getParameter("userPassword");
            try {
                //通过用户账户和用户密码修改账户密码
                manageDao.sureUpPassword(userAccount, userPassword);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            //修改成功后跳转一个新的页面,并给出提示信息
            req.setAttribute("tip", "修改成功,请关闭此页面");
            req.getRequestDispatcher("success.jsp").forward(req, resp);

        }
    }
}
